/////precompiled////////
#include "precompiled.h"
////////////////////////


#include "frm_control_TMover.h"

using namespace frm;
using namespace control;

// TMover /////////////////////////////////////////////////////////////////////

//
TMover::TStyle::TStyle()
{
  Cursor.Set(cursor::TCursorWin(IDC_ARROW));
  Filler.Set(filler::TColorLevelFrame(grf::TColor(0,200,0)));
}

//
TMover::TStyleL::TStyleL()
{
  Cursor.Set(cursor::TCursorWin(IDC_SIZEWE));
}

//
TMover::TStyleT::TStyleT()
{
  Cursor.Set(cursor::TCursorWin(IDC_SIZENS));
}

//
TMover::TStyleR::TStyleR()
{
  Cursor.Set(cursor::TCursorWin(IDC_SIZEWE));
}

//
TMover::TStyleB::TStyleB()
{
  Cursor.Set(cursor::TCursorWin(IDC_SIZENS));
}

//
TMover::TStyleLTRB::TStyleLTRB()
{
  Cursor.Set(cursor::TCursorWin(IDC_SIZEALL));
}

//
TMover::TStyleLT::TStyleLT()
{
  Cursor.Set(cursor::TCursorWin(IDC_SIZENWSE));
}

//
TMover::TStyleRT::TStyleRT()
{
  Cursor.Set(cursor::TCursorWin(IDC_SIZENESW));
}

//
TMover::TStyleLB::TStyleLB()
{
  Cursor.Set(cursor::TCursorWin(IDC_SIZENESW));
}

//
TMover::TStyleRB::TStyleRB()
{
  Cursor.Set(cursor::TCursorWin(IDC_SIZENWSE));
}

//
TMover::TStyles::TStyles() 
{ 
  s[L.GetState().GetBits()] = &L;
  s[T.GetState().GetBits()] = &T;
  s[R.GetState().GetBits()] = &R;
  s[B.GetState().GetBits()] = &B;
 
  s[LTRB.GetState().GetBits()] = &LTRB;
 
  s[LT.GetState().GetBits()] = &LT;
  s[RT.GetState().GetBits()] = &RT;
  s[LB.GetState().GetBits()] = &LB;
  s[RB.GetState().GetBits()] = &RB;
}

//
TMover::TMover(TWindow &Window,TState State) : TPanel(Window)
{
  state = State;

  //Rect.X.Set(20,EDirectChildren_Seq,EAlignChildren_Begin);
  //Rect.Y.Set(20,EDirectChildren_All,EAlignChildren_Begin);
  Width_Fixed(20).Height_Fixed(0).DirectChildren_Horz()
   .WAlignChildren_Begin().HAlignChildren_Begin();

  static TStyles Styles; SetStyle(Styles.Get(State));
}

//
TMover &TMover::SetStyle(TStyle &V)
{
  if (!state.Equal(V.GetState())) { Except("SetStyle[!state.Equal(V.GetState())]");} 
  style = &V; TPanel::SetStyle(V); return *this;
}

//
void TMover::TComponent_Mouse(TMouse Mouse)
{
  bool L = state.Contains(TState::Left);
  bool T = state.Contains(TState::Top);
  bool R = state.Contains(TState::Right);
  bool B = state.Contains(TState::Bottom);

  switch (Mouse.State)
  {
    case grf::TMouse::EState_LButtonDown:
    case grf::TMouse::EState_LButtonDblClick:
    {
      GetMouse().SetCapture(this);
      ptBegin   = Mouse.PtClient;
      rectBegin = GetWindow().GetWindowRect();
    }
    break;

    case grf::TMouse::EState_LButtonUp:
    {
      GetMouse().SetCapture(NULL);
    }
    break;

    case grf::TMouse::EState_Move:
    {
      if (Mouse.Shift.Contains(grf::TShift::Left))
      {
        grf::TPoint Delta = Mouse.PtClient - ptBegin;
        grf::TRect  RNew  = GetWindow().GetWindowRect();

        if (L) { RNew.Left += Delta.X; RNew.Width  -= Delta.X;}
        if (T) { RNew.Top  += Delta.Y; RNew.Height -= Delta.Y;}
        
        if (R) { RNew.Width  = rectBegin.Width  + Delta.X;}
        if (B) { RNew.Height = rectBegin.Height + Delta.Y;}

        int WMin = 200; if (RNew.Width < WMin)
        {
          if (L) { RNew.Left += RNew.Width - WMin;}
          RNew.Width = WMin;
        }
        int HMin = 200; if (RNew.Height < HMin)
        {
          if (T) { RNew.Top += RNew.Height - HMin;}
          RNew.Height = HMin;
        }

        GetWindow().MoveWindow(RNew,true);
      }
    }
    break;
  }                                 
  TPanel::TComponent_Mouse(Mouse);
}
